package com.eternal.base;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.eternal.base.concat.BluetoothEvent;
import com.eternal.base.data.LogRepository;
import com.eternal.base.data.RepositoryInjection;
import com.eternal.base.data.ble.BleStatue;
import com.eternal.base.database.entity.History;
import com.eternal.base.global.BluetoothKey;
import com.eternal.framework.bus.RxBus;
import com.eternal.framework.utils.KLog;

import org.reactivestreams.Publisher;

import java.util.HashMap;
import java.util.Observable;
import java.util.concurrent.TimeUnit;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator
 * On 2020/5/22
 * Description:
 *
 * @author Administrator
 */
public class LogService {
    private static LogService instance = new LogService();
    private LogRepository repository;
    private HashMap<String, Disposable> disposable;

    private LogService() {
        repository = RepositoryInjection.providerLogRepository();
        disposable = new HashMap<>();
    }

    public static LogService getInstance() {
        return instance;
    }

    public void remove(String mac) {
        Disposable temp = disposable.remove(mac);
        if (temp != null) {
            temp.dispose();
        }
    }

    public void clear() {
        for (Disposable temp : disposable.values()) {
            if (temp != null) {
                temp.dispose();
            }
        }
        disposable.clear();
    }

    public void initAndRefresh(final String mac, byte port) {
        final BleStatue statue = repository.getConnect(mac);
        if (statue != null) {
            Disposable temp = repository.initLog(statue, port)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.io())
                    .subscribe(new Action() {
                        @Override
                        public void run() throws Exception {
                            KLog.e("end init refresh");
                            addRefresh(mac, port);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            throwable.printStackTrace();
                            KLog.e(throwable);
                        }
                    });
            disposable.put(mac, temp);
        }
    }

    public Completable addRefresh(@NonNull String mac, byte port) {
        final BleStatue statue = repository.getConnect(mac);
        if (statue == null) {
            return Completable.complete();
        }
        Maybe<Integer> maybe;
        if (statue.getType() == BluetoothKey.DEVICE_TYPE_A || statue.getType() == BluetoothKey.DEVICE_TYPE_B) {
            maybe = repository.refreshLog(statue);
        } else if (statue.getType() == BluetoothKey.DEVICE_TYPE_E) {
            maybe = repository.refreshELog(statue, port);
        } else {
            maybe = repository.refreshCLog(statue);
        }
        return maybe.subscribeOn(Schedulers.io()).doOnSuccess(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                RxBus.getDefault().post(new BluetoothEvent(BluetoothEvent.REFRESH_BADGE, statue.getMac(), integer));
            }
        }).ignoreElement();
    }

}